package com.sanjiahantang.youjiarole.module.me.order.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;
import com.sanjiahantang.youjiarole.module.me.order.views.impl.OrderActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by zy on 2017/9/27.
 */

public class ShopListOrderAdapter extends RecyclerView.Adapter implements StaticValue{

    private Context context;
    private LayoutInflater inflater;
    private Picasso picasso;

    private List<ShopListBean.Data> data;
    public ShopListOrderAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.picasso = Picasso.with(context);
    }

    public List<ShopListBean.Data> getData() {
        return data;
    }

    public void setData(List<ShopListBean.Data> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new OrderHolder(inflater.inflate(R.layout.item_me_order,null), data.get(viewType),viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((OrderHolder)holder).bindData(data.get(position),position);
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data==null ?0:data.size();
    }
    private  TextView btn_be_sure, btn_cancel;
    private class OrderHolder extends RecyclerView.ViewHolder {

        public OrderHolder(View itemView,ShopListBean.Data  data, int position) {
            super(itemView);
            bindData(data,position);
        }

        private void bindData(ShopListBean.Data  data, int position) {
                bindGoodsList(itemView,data);
        }
        private void bindGoodsList(View itemView, final ShopListBean.Data goods) {
            TextView txtOrderno= (TextView) itemView.findViewById(R.id.txtOrderNum);
            TextView txtOrderStatus= (TextView) itemView.findViewById(R.id.txtCreateTime);
            MyListView ServiceOrderListView = (MyListView) itemView.findViewById(R.id.goodListView);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.txtGoodsCount);
            TextView txtMoney = (TextView) itemView.findViewById(R.id.txtAmount);
            TextView txtRemain = (TextView) itemView.findViewById(R.id.txtRemain);
            btn_cancel = (TextView) itemView.findViewById(R.id.txtButton2);
            btn_be_sure = (TextView) itemView.findViewById(R.id.txtButton);
            txtRemain.setVisibility(View.GONE);

            String orderno = null;
            String statusName = goods.getStatusName();

            int count = 0;//数量
            float price = 0f;
            for(ShopListBean.Data.GoodsInfoDtoListBean item : goods.getGoodsInfoDtoList()){
                count += item.getCount();
                price += Float.parseFloat(String.valueOf(item.getPrice())) * item.getCount();
                orderno=item.getOrderRecordCode();
            }

            switch (goods.getStatusId()){
                case OrderView.TYPE_UN_PAY://dai
                    statusName = "待付款";
                    btn_cancel.setText("取消订单");
                    btn_be_sure.setText(String.format("本次应付%.2f元",(float)goods.getPayResult()));
                    initUnPayButton(goods);
                    break;
                case OrderView.TYPE_PART_PAY://bufen
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setText(String.format("本次应付%.2f元",(float)goods.getPayResult()));
                    initPartPayButton(goods);
                    txtRemain.setVisibility(View.VISIBLE);
                    txtRemain.setText(String.format(FORMAT_ORDER_REMAIN,
                            goods.getAreadyResult(),
                            (goods.getPayMoney() - goods.getAreadyResult())));
                    break;
                case OrderView.TYPE_PAYED://yifu
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setVisibility(View.GONE);
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, OrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_PAYED);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });
                    break;
                case OrderView.TYPE_COMPLETE://yiwancheng
                    statusName = "已完成";
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setText("评价");
                    initCompleteButton(goods);
                    break;
                case OrderView.TYPE_CLOSE:
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setVisibility(View.GONE);
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, OrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_CLOSE);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });
                    break;
                case OrderView.TYPE_EVALUATED:
                    statusName = "已完成";
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setVisibility(View.GONE);
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, OrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_EVALUATED);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });
                    break;
                default:
                    statusName = "订单关闭";
                    btn_cancel.setVisibility(View.GONE);
                    btn_be_sure.setVisibility(View.GONE);
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, OrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_CLOSE);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });
                    break;
            }
            txtMoney.setText(String.format("%.2f",price));
            txtGoodsCount.setText(String.valueOf(count));
            txtOrderStatus.setText(statusName);
            txtOrderno.setText(goods.getOrderCode());
            ServiceOrderListView.setAdapter(new QuickAdapter<ShopListBean.Data.GoodsInfoDtoListBean>(context,R.layout.item_order_goods,goods.getGoodsInfoDtoList()) {
                @Override
                protected void convert(BaseAdapterHelper helper, ShopListBean.Data.GoodsInfoDtoListBean item) {
                    helper.setText(R.id.txtTitle,item.getRecordName());
                    helper.setText(R.id.txtPrice,String.format("%.2f",item.getPrice()));
                    if (item.getMgpiValue() != null){
                        TagGroup tagGroup = helper.getView(R.id.tgScale);
                        tagGroup.setTags(item.getMgpiValue());
                    }

                    helper.setText(R.id.txtNumber,String.valueOf(item.getCount()));
                    picasso.load(FileUtils.rePath(item.getUrl()))
                            .placeholder(RES_ID_GOODS_DEFAULT)
                            .into((ImageView) helper.getView(R.id.imgGoods));

                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, OrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_CLOSE);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });
                }
            });
        }

        private void initCompleteButton(final ShopListBean.Data goods) {
            btn_be_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(KEY_TYPE,ORDER_TYPE_COMPLETE);
                    Bundle b = new Bundle();
                    b.putSerializable("goods",goods);
                    intent.putExtra(KEY_PARAM,b);
                    context.startActivity(intent);
                }
            });
        }

        private void initPartPayButton(final ShopListBean.Data goods) {
            btn_be_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(KEY_TYPE,ORDER_TYPE_PART_PAY);
                    Bundle b = new Bundle();
                    b.putSerializable("goods",goods);
                    intent.putExtra(KEY_PARAM,b);
                    context.startActivity(intent);
                }
            });
        }

        private void initUnPayButton(final ShopListBean.Data goods) {
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(KEY_TYPE,ORDER_TYPE_CANCEL);
                    Bundle b = new Bundle();
                    b.putSerializable("goods",goods);
                    intent.putExtra(KEY_PARAM,b);
                    context.startActivity(intent);
                }
            });
            btn_be_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(KEY_TYPE,ORDER_TYPE_PAY);
                    Bundle b = new Bundle();
                    b.putSerializable("goods",goods);
                    intent.putExtra(KEY_PARAM,b);
                    context.startActivity(intent);
                }
            });
        }
    }
}
